﻿using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;

public enum AdType
{
	AdTypeBannerAds= 0x00000AA0,
	AdTypeInterstitialAds= 0x00000AA2,
	AdTypeRewardedAds= 0x00000AA4,
	AdTypeNativeAds= 0x00000AA5,
	AdTypeNone = -1
};

public enum BannerAdPosition
{
	kLayoutTopLeft = 0x000000A0,
	kLayoutTopCenter = 0x000000A1,
	kLayoutTopRight = 0x000000A2,
	kLayoutCenterLeft = 0x000000A3,
	kLayoutCenter = 0x000000A4,
	kLayoutCenterRight = 0x000000A5,

	kLayoutBottomLeft = 0x000000A6,
	kLayoutBottomCenter = 0x000000A7,
	kLayoutBottomRight = 0x000000A8
};

public class AdsManagerScript {

	/* Interface to native implementation */

	#if UNITY_IOS
	[DllImport ("__Internal")]
	private static extern void _SetGameObjectName(string gameObjectName);

//	[DllImport ("__Internal")]
//	private static extern string _GetAdId(int adType);
//
//	[DllImport ("__Internal")]
//	private static extern void _PreloadAd(int adType);
//
//	[DllImport ("__Internal")]
//	private static extern void _PreloadAllAds();

	[DllImport ("__Internal")]
	private static extern bool _ShowAd(int adType);

	[DllImport ("__Internal")]
	private static extern void _RemoveAd(int adType);

//	[DllImport ("__Internal")]
//	private static extern void _SetHidden (int adType, bool isHidden);

	[DllImport ("__Internal")]
	private static extern void _SetPosition (int adType, int positionType);

	[DllImport ("__Internal")]
	private static extern float _GetBannerWidth();

	[DllImport ("__Internal")]
	private static extern float _GetBannerHeight();

	[DllImport ("__Internal")]
	private static extern void _UnityIsReady();

	#endif


	public void SetGameObjectName(string gameObjectName) 
	{

		// Call plugin only when running on real device
		if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
			#if UNITY_ANDROID
			SSCAdsUnityAdapter.getInstance().setGameObjName(gameObjectName);
			#elif UNITY_IOS
			_SetGameObjectName(gameObjectName);
			#endif
		}
	}

//	public string GetAdId(int adType) 
//	{
//		// Call plugin only when running on real device
//		if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
//			#if UNITY_ANDROID
//			return SSCAdsUnityAdapter.getInstance().getAdid(adType);
//			#elif UNITY_IOS
//			return _GetAdId(adType);
//			#endif
//		}
//		return "";
//	}

//	public void PreloadAd(int adType)
//	{
//		// Call plugin only when running on real device
//		if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
//			#if UNITY_ANDROID
//			SSCAdsUnityAdapter.getInstance().preloadAd(adType);
//			#elif UNITY_IOS
//			_PreloadAd(adType);
//			#endif
//		}
//	}

//	public void PreloadAllAds()
//	{
//		// Call plugin only when running on real device
//		if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
//			#if UNITY_ANDROID
//			SSCAdsUnityAdapter.getInstance().preloadAll();
//			#elif UNITY_IOS
//			_PreloadAllAds();
//			#endif
//		}
//			
//	}

	public bool ShowAd(int adType)
	{
		// Call plugin only when running on real device
		if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
			#if UNITY_ANDROID
			return SSCAdsUnityAdapter.getInstance().showAd(adType);
			#elif UNITY_IOS
			return _ShowAd(adType);
			#endif
		}

		return false;
	}

	public void RemoveAd(int adType)
	{
		// Call plugin only when running on real device
		if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
			#if UNITY_ANDROID
			SSCAdsUnityAdapter.getInstance().removeAd(adType);
			#elif UNITY_IOS
			_RemoveAd(adType);
			#endif
		}
			
	}

//	public void SetHidden (int adType, bool isHidden)
//	{
//		// Call plugin only when running on real device
//		if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
//			#if UNITY_ANDROID
//			SSCAdsUnityAdapter.getInstance().setHidden(adType,isHidden);
//			#elif UNITY_IOS
//			_SetHidden(adType, isHidden);
//			#endif
//		}
//	}

	public void SetPosition (int adType, int positionType)
	{
		// Call plugin only when running on real device
		if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
			#if UNITY_ANDROID
			SSCAdsUnityAdapter.getInstance().setPosition(adType,positionType);
			#elif UNITY_IOS
			_SetPosition(adType, positionType);
			#endif
		}
	}

	public Vector2 GetBannerSize ()
	{
		// Call plugin only when running on real device
		if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
			#if UNITY_ANDROID
			return SSCAdsUnityAdapter.getInstance().getBannerSize((int)AdType.AdTypeBannerAds);
			#elif UNITY_IOS
				float width = _GetBannerWidth();
				float height = _GetBannerHeight();
				return new Vector2(width,height);
			#endif
		}

		return new Vector2(0,0);
	}

	public void UnityIsReady()
	{
		// Call plugin only when running on real device
		if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
			#if UNITY_ANDROID
			#elif UNITY_IOS
				_UnityIsReady();
			#endif
		}
	}
}
